package net.osweb.core.fw.service;

import java.util.Date;

import net.osweb.core.fw.dto.BaseDto;
import net.osweb.core.fw.exception.CacheException;
import net.osweb.core.tools.utils.date.DateUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/**
 * liwu E-mail:
 * 
 * @version 创建时间：Jun 3, 2011 2:56:50 PM 类说明
 */
public class PojoCacheOperator {

	protected static final Log log = LogFactory.getLog(PojoCacheOperator.class);
	private static MemCachedManager cache = MemCachedManager.getInstance();
	private static final String CACHETYPE_POJO = "POJO";

	public static void add2Cache(BaseDto dto) throws CacheException {
		try {
			if (null != dto) {
				Long id = dto.getId();
				if (null != id && id > 0) {
					Long cacheExpiry = dto.getCacheExpiry();
					String key = CACHETYPE_POJO + dto.getClass().getName() + id;
					Date expiryDate;
					if (cache.exists(key)) { // pojo已经存在于缓存，则替换。
						if (null != cacheExpiry && cacheExpiry > 0) {
//							expiryDate = DateUtil.getExpiryDateTime(new Date().toString(), cacheExpiry);
							expiryDate = DateUtil.getExpiryDateTime(new Date(), cacheExpiry);
							cache.replace(CACHETYPE_POJO
									+ dto.getClass().getName() + id, dto,
									expiryDate);
							log
									.debug("have replaced a pojo to cache : class is "
											+ dto.getClass().getName()
											+ ";id is "
											+ id
											+ ";expiryDate is "
											+ expiryDate.toString());
						} else { // pojo已经不存在于缓存，则添加。
							cache.replace(CACHETYPE_POJO
									+ dto.getClass().getName() + id, dto);
							log
									.debug("have replaced a pojo to cache : class is "
											+ dto.getClass().getName()
											+ ";id is " + id);
						}
					} else {
						if (null != cacheExpiry && cacheExpiry > 0) {
//							expiryDate = DateUtil.getExpiryDateTime(new Date().toString(), cacheExpiry);
							expiryDate = DateUtil.getExpiryDateTime(new Date(), cacheExpiry);
							cache.add(CACHETYPE_POJO + dto.getClass().getName()
									+ id, dto, expiryDate);
							log
									.debug("have added a pojo to cache : class is "
											+ dto.getClass().getName()
											+ ";id is "
											+ id
											+ ";expiryDate is "
											+ expiryDate.toString());
						} else { // pojo已经不存在于缓存，则添加。
							cache.add(CACHETYPE_POJO + dto.getClass().getName()
									+ id, dto);
							log
									.debug("have added a pojo to cache : class is "
											+ dto.getClass().getName()
											+ ";id is " + id);
						}
					}
				}
			}
		} catch (Exception ex) {
			log.error("add pojo to cache Error:" + ex);
			throw new CacheException(ex);
		}
	}

	public static BaseDto getFromCache(BaseDto dto) throws CacheException {
		if (null == dto)
			return null;
		try {
			dto = (BaseDto) cache.get(CACHETYPE_POJO + dto.getClass().getName()
					+ dto.getId());
			if(log.isDebugEnabled() && dto !=null){
			log.debug("have get a pojo from cache : class is "
					+ dto.getClass().getName() + ";id is " + dto.getId());
			}
			return dto;
		} catch (Exception ex) {
			log.error("get pojo from cache Error:" + ex);
			throw new CacheException(ex);
		}
	}

	public static void removeFromCache(BaseDto dto) throws CacheException {
		if (null != dto)
			try {
				cache.delete(CACHETYPE_POJO + dto.getClass().getName()
						+ dto.getId());
				log.debug("have removed a pojo from cache : class is "
						+ dto.getClass().getName() + ";id is " + dto.getId());
			} catch (Exception ex) {
				log.error("remove pojo from cache Error:" + ex);
				throw new CacheException(ex);
			}
	}
	
	public static void replaceCache(BaseDto dto) throws CacheException {
		if (null != dto)
			try {
				Date expiryDate;
				Long cacheExpiry = dto.getCacheExpiry();
				if (null != cacheExpiry && cacheExpiry > 0) {
//					expiryDate = DateUtil.getExpiryDateTime(new Date().toString(), cacheExpiry);
					expiryDate = DateUtil.getExpiryDateTime(new Date(), cacheExpiry);
					cache.replace(CACHETYPE_POJO + dto.getClass().getName()
							+ dto.getId() , dto,expiryDate );
					log.debug("have replace a pojo from cache : class is "
							+ dto.getClass().getName() + ";id is " + dto.getId()+ ";expiryDate is "
							+ expiryDate.toString());
				}else{
					cache.replace(CACHETYPE_POJO + dto.getClass().getName()
							+ dto.getId() , dto );
					log.debug("have replace a pojo from cache : class is "
							+ dto.getClass().getName() + ";id is " + dto.getId());
				}
				
			} catch (Exception ex) {
				log.error("replace pojo from cache Error:" + ex);
				throw new CacheException(ex);
			}
	}
	
}
